package hu.uszeged.inf.stud.prf.todo.entity.resource;

import hu.uszeged.inf.stud.prf.todo.entity.task.Task;
import hu.uszeged.inf.stud.prf.todo.entity.user.User;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "resource",
	uniqueConstraints = @UniqueConstraint(columnNames={"name", "user_id", "archive"} ) )
public class Resource implements Serializable {
	
	private static final long serialVersionUID = -5635645173372982375L;

	@Id
	@SequenceGenerator(name = "idGenSeqResource", initialValue=5)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenSeqResource")
	private Long id;
	
	private String name;
	
	private Boolean archive;

	@ManyToMany(cascade = CascadeType.ALL,
			mappedBy = "resources")
	private List<Task> tasks;
	
	@ManyToOne
	private User user;

	public Resource() {
		super();
		this.archive = false;
	}

	public Resource(Long id, String name, Boolean archive, List<Task> tasks,
			User user) {
		super();
		this.id = id;
		this.name = name;
		this.archive = archive;
		this.tasks = tasks;
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean isArchive() {
		return archive;
	}

	public void setArchive(Boolean archive) {
		this.archive = archive;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((archive == null) ? 0 : archive.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public String toString() {
		return name;
	}

}
